﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;

namespace Utility
{
    public class Win32API
    {
        [DllImport("user32.dll ", CharSet = CharSet.Unicode)]
        public static extern IntPtr PostMessage(IntPtr hwnd, int wMsg, string wParam, string lParam);
    }

    /// <summary>
    /// 自定义Exception
    /// </summary>
    public class SystemException : Exception
    {
        #region 包含系统Excepton
        /// <summary>
        /// 包含系统Excepton
        /// </summary>
        public SystemException(string source, string message, Exception inner)
            : base(message, inner)
        {
            base.Source = source;
        }
        #endregion

        #region 不包含系统Excepton
        /// <summary>
        /// 不包含系统Excepton
        /// </summary>			
        public SystemException(string source, string message)
            : base(message)
        {
            base.Source = source;
        }
        #endregion
    }

    /// <summary>
    /// SystemTools
    /// </summary>
    public class SystemTools
    {

        #region  获得当前工作路径
        /// <summary>
        ///  获得当前工作路径
        /// </summary>
        /// <returns></returns>
        public static string GetCurrentDirectory()
        {
            string fullAppName = Assembly.GetExecutingAssembly().GetName().CodeBase;
            string fullAppPath = Path.GetDirectoryName(fullAppName);
            if (fullAppPath.StartsWith("file:"))
            {
                fullAppPath = fullAppPath.Substring(6);
            }
            return (fullAppPath);
        }
        #endregion

        #region 获得存储磁盘的剩余空间
        /// <summary>
        /// 获得存储磁盘的剩余空间
        /// </summary>
        /// <param name="disk"></param>
        /// <returns></returns>
        public static string getDiskAviSize(string disk)
        {
            string size = "";

            System.IO.DriveInfo[] drive = System.IO.DriveInfo.GetDrives();

            for (int i = 0; i < drive.Length; i++)
            {
                if (drive[i].Name.ToLower().Equals(disk.ToLower()))
                {
                    if (drive[i].AvailableFreeSpace < 1024)
                    {
                        size = Convert.ToString(drive[i].AvailableFreeSpace) + "B";
                    }
                    else if (drive[i].AvailableFreeSpace < 1024 * 1024)
                    {
                        size = Convert.ToString(drive[i].AvailableFreeSpace / 1024) + "KB";

                    }
                    else if (drive[i].AvailableFreeSpace < 1024 * 1024 * 1024)
                    {
                        size = Convert.ToString(drive[i].AvailableFreeSpace / (1024 * 1024)) + "MB";
                    }
                    else
                    {
                        size = Convert.ToString(drive[i].AvailableFreeSpace / (1024 * 1024 * 1024)) + "GB";
                    }
                }
            }
            return size;
        }
        #endregion

    }
}
